<?php
	///////////////////////
	// GLOBAL PARAMETERS //
	///////////////////////
	
	$intSpaceBetweenWorkAndHomeEmailX = 150;
	$intSpaceBetweenWorkAndMobNumX = 85;
	$intLineSepBetFmlySize = 20;
	$intSmallLineSepSize = 20;
	$intLineSepSize = 26;
	$intFirstNameIndentX = 165;
	$intMinLeftPositionX = 50;
	$HomeAddrHeightY = 90;

	$intLeftDetailsIndentX = 40;
	$intFirstDetailsX = 168;

	$intLeftEmailDetailsIndentX = $intFirstDetailsX + 150;
	$intLeftEmailDetailsIndentX = 300;

	$intIndvRoleIndentX = 300;
	$intHomeTelephoneIndentX = 360;
	$intLeftDetailsX = 80;
	$intMinSepAfterAddrY = 1;    // spacing between families
	$intPageCountPosY = 8;
	$intImageWidth = 110;

	//////////////////////////
	// INITIALISE VARIABLES //
	//////////////////////////
	$maxFamilySize = 0;
	$bottom_image_Y = 0;
	$intTopOfAddressY = 0;
	$CSVOutput = "";
	$CSVOutput_Indv = "";
	$CSVOutput_Fmly = "";
	$bCSV_FirstRow = TRUE;
	$CSV_FirstRow = "";




	///////////////////////
	//   font            //
	///////////////////////
	$NormalFont = "Helvetica";
	$BoldFont = "Helvetica-Bold";
	$tinyFontSize = 14; // orig 8     // Role
	$smallFontSize = 16; // orig 10  // address
	$mediumFontSize = 20; // orig 12 // first Name
	$largeFontSize = 24; // orig 16  // Surname and Home PhoneNumber
	$hugeFontSize = 40; // orig 40

	///////////////////////
	//   PAGE SIZE (A4)  //
	///////////////////////
	$intPageSizeX = 595;  // ISO Standard A4 width
	$intPageSizeY = 842;  // ISO Standard A4 length
	$intTopOfPageY = 800; // maximum top of page to use
	$intTopOfPageX = 45;  // minimum left of page to use
	$intMinBottomPageY = 0;  // minimum bottom of page to use  42

	///////////////////////
	// LINE CONSTRAINTS  //
	///////////////////////
	$intLineSpaceToLeaveAtBothEnds = 40;
	$intLineSepLeftX = $intLineSpaceToLeaveAtBothEnds;
	$intLineSepRightX = $intPageSizeX - $intLineSpaceToLeaveAtBothEnds-10; // 595 is A4 page width

	///////////////////////////
	// DEFAULT ITEMS TO SHOW //
	///////////////////////////
	$bShowPrivateResidentialAddressesAsXXX = TRUE;  // done
	$bShowPrivateTelephoneNumbersAsXXX = TRUE;      // done
	$bShowPrivateEmailAddressesAsXXX = TRUE;		//done
	$bShowFamilyPicture = FALSE;
	$bShowRole = FALSE;
	$bShowGender = FALSE;
	$bShowWorkPhoneNumber = FALSE;
	$bShowWorkEmailAddress = FALSE;
	$bShowHomeEmailAddress = FALSE;
	$bShowMobilePhoneNumber = FALSE;
    $bShowSchoolGrade = FALSE;
	$bShowTitlePage = FALSE;
	$bShowPageNumber = FALSE;
	$bHidePrivateData = TRUE;
	$bCSVOutput = TRUE;
	$bXMLOutput = FALSE;
	//////////////////////////////////////////////////////
	// GPAC Database is Copyright 2003 Colin Todd       //
	// Written by Colin Todd - All rights reserved.     //
	//                                                  //
	// colintodd@myfastmail.com                         //
	//////////////////////////////////////////////////////

	$root = '../';
	include($root.'include/dbconnect.php');
//	check user is logged in
//	include_once('_includes/master_include.php');

	include($root .'include/config.php');
	include($root.'include/functions.php');

	$bShowPrivateResidentialAddressesAsXXX = @$_GET['ShowPrivateRA'];
	$bShowPrivateTelephoneNumbersAsXXX = @$_GET['ShowPrivatePN'];
	$bShowPrivateEmailAddressesAsXXX = @$_GET['ShowPrivateEA'];
	$bShowFamilyPicture = @$_GET['ShowFmlyPic'];
	$bShowRole = @$_GET['ShowRole'];
	$bShowGender = @$_GET['ShowGender'];
	$bShowWorkPhoneNumber = @$_GET['ShowWrkPN'];
	$bShowWorkEmailAddress = @$_GET['ShowWrkEA'];
	$bShowHomeEmailAddress = @$_GET['ShowHomeEA'];
	$bShowMobilePhoneNumber = @$_GET['ShowMob'];
    $bShowSchoolGrade = @$_GET['ShowSchoolGde'];
	$bShowTitlePage = @$_GET['ShowTitlePage'];
	$bShowPageNumber = @$_GET['ShowPageNum'];
	$intPageCount = @$_GET['StartPageNum']-1; // set count to 0
	$bHidePrivateData = @$_GET['HidePrivateData'];
	if(@$_GET['CSVOutput']=="FALSE") {
		$bCSVOutput = FALSE;
	} else {
		$bCSVOutput = TRUE;
	}
	if(@$_GET['XMLOutput']=="FALSE") {
		$bXMLOutput = FALSE;
	} else {
		$bXMLOutput = TRUE;
	}


//	$bXMLOutput = @$_GET['XMLOutput'];
	
	
//	echo "csv ".$bCSVOutput."<p>";
//	echo "xml ".$bXMLOutput;
//exit;
//	echo "<pre>";

	/*
	echo "<pre>";
	print_r(@$_GET);
	echo "</pre>";
	*/

	$Indv_sql = html_entity_decode(@$_GET['indv_sql']);
	$Indv_sql = urldecode($Indv_sql);

	$Fmly_sql = html_entity_decode(@$_GET['fmly_sql']);
	$Fmly_sql  = urldecode($Fmly_sql);


//$CSV_FirstRow = $Indv_sql."\n";
//$CSV_FirstRow = $Fmly_sql."\n";


	if(strlen(trim($Fmly_sql))==0) {
		$Fmly_sql = "TRUE";
	}
	if(strlen(trim($Indv_sql))==0) {
		$Indv_sql = "TRUE";
	}

	$sql_Fmly = "SELECT Family.FamilyId, Family.CommentText, Family.FamilyShortName FROM individual RIGHT JOIN Family ON individual.FamilyId = Family.FamilyId WHERE $Fmly_sql AND $Indv_sql GROUP BY Family.FamilyId ORDER BY Family.FamilyShortName ASC";

//echo "\n";
//echo "SQL::".$sql_Fmly."\n";


//	$sql_Fmly = "SELECT * FROM Family WHERE  $Fmly_sql ORDER BY Family.FamilyShortName  ASC";
//SELECT Family.FamilyId, Family.CommentText Family.FamilyShortName FROM individual RIGHT JOIN Family ON individual.FamilyId = Family.FamilyId WHERE $Fmly_sql AND $Indv_sql GROUP BY Family.FamilyId;
	$result_Fmly = mysql_query($sql_Fmly);
	$resultsnumber_Fmly = 0;
	$resultsnumber_Fmly = @mysql_num_rows($result_Fmly);

$FmlyCount = 0;
$IndvCount = 0;
	if($resultsnumber_Fmly >0) {
//		echo mysql_num_rows($result_Fmly)."<p>";
		while ($row_Fmly = mysql_fetch_assoc ($result_Fmly)) { 
			//
			// Grab Family details
			//

//			if ($count > 200 ){  break;}
			$FmlyCount = $FmlyCount + 1;
			$FamilyId = "";
			$Fmly_CommentText = "";
			$Fmly_FamilyShortName = "";
			$Fmly_ResAddress = "";
			$Fmly_HomePhone = "";

			$Indv_MaleName = "";
			$Indv_MaleSurname = "";
			$Indv_MaleMobilePhoneNum = "";
			$Indv_MaleChurchIntroMethod  = "";
			$Indv_MaleChurchIntroComment = "";

			$Indv_FemaleName = "";
			$Indv_FemaleSurname = "";
			$Indv_FemaleMobilePhoneNum = "";
			$Indv_FemaleChurchIntroMethod  = "";
			$Indv_FemaleChurchIntroComment = "";

			$FamilyId = $row_Fmly["FamilyId"];
			$Fmly_FamilyShortName = $row_Fmly["FamilyShortName"];
			$Fmly_ResAddress  = getFormattedResidentialAddress($FamilyId, " ",TRUE,FALSE,FALSE,FALSE ,FALSE , "", $bShowPrivateResidentialAddressesAsXXX, FALSE,FALSE," ");
			$Fmly_HomePhone     = getFormattedHomePhoneNumber($FamilyId," ",FALSE, FALSE, FALSE, "", $bShowPrivateTelephoneNumbersAsXXX);
		    $Fmly_CommentText  = $row_Fmly["CommentText"];

//print_r($row_Fmly);
//echo "\n";


// Get MALE head of family details
			$sql_Indv = "SELECT * FROM Individual WHERE FamilyId = $FamilyId AND $Indv_sql AND RoleTypeCode='Head Of Family' AND GenderCode='M';";
			$result_Indv = @mysql_query($sql_Indv);
			$row_result_Indv = @mysql_fetch_assoc($result_Indv);
/*
echo "\n";
echo "SQL::".$sql_Indv."\n";
print_r($row_result_Indv);
echo "\n";
*/
			$Indv_MaleName =  @$row_result_Indv["FirstName"];
			$Indv_MaleSurname = @$row_result_Indv["Surname"];
			$Indv_MaleMobilePhoneNum = @getFormattedMobilePhoneNumber(@$row_result_Indv ["IndividualId"]," ", FALSE, FALSE ,"", $bShowPrivateTelephoneNumbersAsXXX);
			$Indv_MaleChurchIntroMethod = @$row_result_Indv["ChurchIntroductionMethod"];
			$Indv_MaleChurchIntroComment  = @$row_result_Indv["ChurchIntroductionComment"];

// Get FEMALE head of family details
   		    $sql_Indv = "SELECT * FROM Individual WHERE FamilyId = $FamilyId AND $Indv_sql AND RoleTypeCode='Head Of Family' AND GenderCode='F';";
			$result_Indv = @mysql_query($sql_Indv);
			$row_result_Indv = @mysql_fetch_assoc($result_Indv);
/*
echo "\n";
echo "SQL::".$sql_Indv."\n";
print_r($row_result_Indv);
echo "\n";
*/
			$Indv_FemaleName =  @$row_result_Indv["FirstName"];
			$Indv_FemaleSurname = @$row_result_Indv["Surname"];
			$Indv_FemaleMobilePhoneNum = @getFormattedMobilePhoneNumber(@$row_result_Indv ["IndividualId"]," ", FALSE, FALSE ,"", $bShowPrivateTelephoneNumbersAsXXX);
			$Indv_FemaleChurchIntroMethod = @$row_result_Indv["ChurchIntroductionMethod"];
			$Indv_FemaleChurchIntroComment  = @$row_result_Indv["ChurchIntroductionComment"];

			if (strlen($Indv_FemaleName)>0){
				$IndvCount = $IndvCount + 1;
			}

			if (strlen($Indv_MaleName)>0){
				$IndvCount = $IndvCount + 1;
			}




// Columns to insert
				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW FAMILY SURNAME
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= "Surname";
					}
					$CSVOutput_Fmly .= $Fmly_FamilyShortName;					
				}
				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW MALE FIRSTNAME
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= ",Male";
					}
					$CSVOutput_Fmly .= ",".$Indv_MaleName;
				}
				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW MALE MOBILE PHONE NUMBER
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= ",Mobile";
					}
					$CSVOutput_Fmly .= ",=\"\" & \"".$Indv_MaleMobilePhoneNum."\"";
				}
				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW FEMALE FIRSTNAME
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= ",Female";
					}
					$CSVOutput_Fmly .= ",".$Indv_FemaleName;
				}
				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW FEMALE MOBILE PHONE NUMBER
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= ",Mobile";
					}
					$CSVOutput_Fmly .= ",=\"\" & \"".$Indv_FemaleMobilePhoneNum."\"";
				}
				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW HOME PHONE
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= ",HomePhone";
					}
					$CSVOutput_Fmly .= ",=\"\" & \"".$Fmly_HomePhone."\"";
				}

				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW RESIDENTIAL ADDRESS
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= ",Address";
					}
					$CSVOutput_Fmly .= ",".$Fmly_ResAddress;
				}

				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW FAMILY COMMENT
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= ",Comment";
					}
					$CSVOutput_Fmly .= ",\"".$Fmly_CommentText."\"";
				}


				if(!$bCSVOutput and !$bXMLOutput) {
	// SHOW INDV INTRO TO CHURCH
				} else {
					if($bCSV_FirstRow) {
						$CSV_FirstRow .= ",IntroToChurch";
					}
						$IntroCommentText="";
						if ($Indv_MaleChurchIntroComment==$Indv_FemaleChurchIntroComment){
							$IntroCommentText=$Indv_MaleChurchIntroComment;
						} else
						{
							$IntroCommentText=$Indv_MaleChurchIntroComment."; ".$Indv_FemaleChurchIntroComment;
						}
					$CSVOutput_Fmly .= ",\"".$IntroCommentText."\"";
				}



				//
				// Insert line at end of Family
				//

				$CSVOutput = $CSVOutput."\n".$CSVOutput_Fmly ;

				$CSVOutput_Fmly = "";
				$CSVOutput_Indv = "";
				$bCSV_FirstRow = FALSE;
		} // end while family
	} // end if rowcount


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(!$bCSVOutput and !$bXMLOutput) {

		// Middle footer
	} else {
		$Header = "";

		$Header .= "Date: ".$date."\n";

		$Header .= "Number of Families Listed: ".$FmlyCount."\n";
		$Header .= "Number of Individuals Listed: ".$IndvCount."\n";
		
		$CSVOutput = $Header."\n".$CSV_FirstRow."\n".$CSVOutput;

if($bCSVOutput) {
	header("Content-type: text/csv");
	header("Content-Length: ".strlen($CSVOutput));
	header("Content-Disposition: attachment; filename=report".date("His").".csv");
	echo $CSVOutput;
	exit;
} else

		{
			echo $CSVOutput;
		}
	}
?>

